define(["angularAMD"], function (angularAMD) {
    angularAMD.service('B0237Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            this.init = function (options) {

                // 取得工藝特性資訊收集資料
                function craftsCharacteristicInfoGet(callback) {
                    // var data = {
                    //     machine_no: 'Machine01',
                    //     machine_name: '設備一',
                    //     production_inf_get:[
                    //         {
                    //             tabpage_display: "退火",
                    //             production_inf_detail:[
                    //                 {
                    //                     productioninf_no: "001",
                    //                     productioninf_name: "轉速",
                    //                     description: "說明(退火)",
                    //                     method: "2"
                    //                 },
                    //                 {
                    //                     productioninf_no: "002",
                    //                     productioninf_name: "溫度",
                    //                     description: "說明(退火)",
                    //                     method: "3"
                    //                 },
                    //                 {
                    //                     productioninf_no: "003",
                    //                     productioninf_name: "電流",
                    //                     description: "說明(退火)",
                    //                     method: "1"
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             tabpage_display: "正火",
                    //             production_inf_detail:[
                    //                 {
                    //                     productioninf_no: "001",
                    //                     productioninf_name: "溫度",
                    //                     description: "說明(正火)",
                    //                     method: "3"
                    //                 },
                    //                 {
                    //                     productioninf_no: "002",
                    //                     productioninf_name: "轉速",
                    //                     description: "說明(正火)",
                    //                     method: "2"
                    //                 },
                    //                 {
                    //                     productioninf_no: "003",
                    //                     productioninf_name: "電流",
                    //                     description: "說明(正火)",
                    //                     method: "1"
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             tabpage_display: "淬火",
                    //             production_inf_detail:[
                    //                 {
                    //                     productioninf_no: "001",
                    //                     productioninf_name: "電流",
                    //                     description: "說明(淬火)",
                    //                     method: "1"
                    //                 },
                    //                 {
                    //                     productioninf_no: "002",
                    //                     productioninf_name: "溫度",
                    //                     description: "說明(淬火)",
                    //                     method: "3"
                    //                 },
                    //                 {
                    //                     productioninf_no: "003",
                    //                     productioninf_name: "轉速",
                    //                     description: "說明(淬火)",
                    //                     method: "2"
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             tabpage_display: "回火",
                    //             production_inf_detail:[
                    //                 {
                    //                     productioninf_no: "001",
                    //                     productioninf_name: "溫度",
                    //                     description: "說明(回火)",
                    //                     method: "3"
                    //                 },
                    //                 {
                    //                     productioninf_no: "002",
                    //                     productioninf_name: "電流",
                    //                     description: "說明(回火)",
                    //                     method: "1"
                    //                 },
                    //                 {
                    //                     productioninf_no: "003",
                    //                     productioninf_name: "轉速",
                    //                     description: "說明(回火)",
                    //                     method: "2"
                    //                 }
                    //             ]
                    //         }
                    //     ]
                    // };
                    // if(callback)
                    //     callback(data);
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_WIP.production_inf_get',
                        content: {
                            op_no: options.op_no,
                            plot_no: options.plot_no,
                            machine_no: options.machine_no,
                            op_plug_in: options.op_plug_in
                        },
                        success: function (data) {
                            if(data.production_inf_get && data.production_inf_get.length > 0){
                                if(callback)
                                    callback(data);
                            } else {
                                $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                            }
                        }
                    });
                };

                var openDialog = function(data){
                    $mdDialog.dialog(
                    'module/moduleB/program/B02/B0237/B0237.html', function(dialog){
                        return {
                            css_url: 'module/moduleB/program/B02/B0237/B0237.css',
                            collectionInfo:{
                                heat_plot_no: options.heat_plot_no,
                                employee_no: config.cache.account,
                                inspect_time: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                                machine_no: options.machine_no,
                                machine_name: options.machine_name
                            },
                            production_inf_get: data.production_inf_get,
                            now_tabpage_display: data.production_inf_get[0].tabpage_display,
                            now_production_inf_detail: data.production_inf_get[0].production_inf_detail,
                            back: function(){
                                dialog.hide();
                            },
                            loadEmployeeNo: function(){
                                // 20200527 modify by WeiTing for M#72503 - 工藝插件熱處理 - 工作平台 - 工藝特性資訊收集泡泡按鈕 - 執行人員開窗服務，SA 少了此規格，所以先不接服務。
                            },
                            changeProductionPhase: function(item){
                                dialog.now_tabpage_display = item.tabpage_display;
                                dialog.now_production_inf_detail = item.production_inf_detail;
                                // 修改滑動按鈕底層 transition 導致畫面切換造成的延遲問題
                                $('.check-point-switch').css('transitionDuration', '0s');
                            },
                            confirm: function(){
                                $MMWService.sendToServer({
                                    uri: 'ServicesSTD.Module_WIP.production_inf_process',
                                    content: {
                                        machine_no: dialog.collectionInfo.machine_no,
                                        machine_name: dialog.collectionInfo.machine_name,
                                        production_inf_get: dialog.production_inf_get,
                                        inspect_time: dialog.collectionInfo.inspect_time,
                                        employee_no: dialog.collectionInfo.employee_no,
                                        heat_plot_no: dialog.heat_plot_no,
                                        op_no: options.op_no,
                                        plot_no: options.plot_no,
                                    },
                                    success: function () {
                                        dialog.hide();
                                        $rootScope.showToast($filter('translate')('common.msg.success'));
                                        if(options.feedback)
                                            options.feedback();
                                    }
                                });
                            }
                        };
                    });
                };

                craftsCharacteristicInfoGet(function(data){
                    openDialog(data);
                });

            };
        }]);
});
